import {EventType} from '../global/constast';
export class Singleton {
    private static instance: Singleton;
    private eventMap:any = {
    };
    private constructor() {
        window.addEventListener(EventType.message,(event:any)=>{
            let moduleType = event.data.pluginMessage.moduleType;
            if(moduleType){
                this.emitModuleEvent(event,moduleType);
            }
            
        });
    };
    /**
     * 发布模块消息
     * @param event
     */
    private emitModuleEvent(event,moduleType){
        if(this.eventMap[moduleType]){
           for(let eventFn of this.eventMap[moduleType]){
               eventFn(event);
           }
        }
    }
    public static getInstance(): Singleton {
        if (!Singleton.instance) {
            Singleton.instance = new Singleton();
        }
        return Singleton.instance;
    }
    public on(name:string,fn:Function){
        if(!this.eventMap[name]){
            this.eventMap[name] = [];
        }
         this.eventMap[name].push(fn);
    }
}
 
// 获取单例对象
export const messageInstance = Singleton.getInstance();